<?php
//submitAssignment.php
//provides the student the ability to upload a submission - rjs
$allow=true;
require_once("overhead.php");

$assignmentID=mysql_real_escape_string($_REQUEST['aid']);
$getAssignmentInfoQuery=mysql_query("select Status from assignment where ID=".$assignmentID);
$row=mysql_fetch_assoc($getAssignmentInfoQuery);
if ($row["Status"] != "Open") {
	header("Location: viewAssignment.php?msg=Submissions for this assignment are no longer accepted.&aid=".$assignmentID);
	exit;
}

if ($_FILES["submissionFile"]["error"] > 0) {
  echo "Error: " . $_FILES["submissionFile"]["error"] . "<br />";
  } else {
  
  	if (!file_exists("assignments/assignment_".$assignmentID."/submissions/".escapeshellcmd($currentUser))) {
  		if (!mkdir("assignments/assignment_".$assignmentID."/submissions/".escapeshellcmd($currentUser),0770)) {
  			throwError(3);
  		}
  	}
  	move_uploaded_file($_FILES["submissionFile"]["tmp_name"],"assignments/assignment_".$assignmentID."/submissions/".escapeshellcmd($currentUser)."/".escapeshellcmd($currentUser)."_".$assignmentID.".zip");
	$submitAssignmentQuery=mysql_query("insert into submission (AssignmentID,Score,UserID,File,Date) values (".$assignmentID.",null,'".$currentUser."', null,NOW())");
	if ($submitAssignmentQuery) {
		header("Location: viewAssignment.php?msg=Assignment (".$_FILES['submissionFile']["name"].") Submitted!.&aid=".$assignmentID);
		exit;
	}
	else {
		header("Location: viewAssignment.php?msg=There was a problem with your submission.&aid=".$assignmentID);
		exit;
	}
 }

//We need to check to make sure that the user has not already submitted!

/*$submitAssignmentQuery=mysql_query("insert into submission (AssignmentID,Score,UserID,File,Date) values (".$assignmentID.",null,'".$currentUser."', null,NOW())");
if ($submitAssignmentQuery) {
	header("Location: viewAssignment.php?cid=".$currentCourse."&uid=".$currentUser."&role=".$currentRole."&msg=Assignment Submitted!.&aid=".$assignmentID);
	exit;
}
else {
	header("Location: viewAssignment.php?cid=".$currentCourse."&uid=".$currentUser."&role=".$currentRole."&msg=There was a problem with your submission.&aid=".$assignmentID);
	exit;
}
*/

?>